package com.tyl.service.provider;

import com.tyl.dao.BaseDao;
import com.tyl.dao.bill.BillDao;
import com.tyl.dao.bill.BillDaoImpl;
import com.tyl.dao.provider.ProviderDao;
import com.tyl.dao.provider.ProviderDaoImpl;
import com.tyl.pojo.Provider;
import com.tyl.servlet.provider.ProviderServlet;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{

    /**
     *  通过供应商名称、编码获取供应商列表-模糊查询-providerList
     * **/
    private ProviderDao providerDao;

    private BillDao billDao;
    public ProviderServiceImpl() {
        providerDao=new ProviderDaoImpl();
        billDao=new BillDaoImpl();
    }
    /**
     *  通过供应商名称、编码获取供应商列表-模糊查询-providerList
     * **/
    public List<Provider> getProviderList(String proCode, String proName) {
        Connection connection=null;
        connection = BaseDao.getConnection();
        List<Provider> providerList= null;
        if (connection!=null){
            try {
                providerList = providerDao.getProviderList(connection, proCode, proName);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return providerList;
    }
    /**
     * 通过proId获取Provider
     * **/
    public Provider getProviderById(String id){
        Connection connection=null;
        connection= BaseDao.getConnection();
        Provider provider=null;
        if(connection!=null){
            try {
                provider = providerDao.getProviderById(connection, id);
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return provider;
    }
    /**
     * 添加供应商
     * **/
    public int addProvider(Provider provider) {
        Connection connection=null;
        connection= BaseDao.getConnection();
        int flag=0;
        if (connection!=null){
            try {
                connection.setAutoCommit(false);//开启事务
                flag= providerDao.addProvider(connection, provider);
                connection.commit();//提交事务
            } catch (SQLException e) {
                e.printStackTrace();
                try {
                    connection.rollback();//事务回滚
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return flag;
    }
    /**
     * 删除供应商
     * **/
    public int deleteProvider(String pId) {
        Connection connection=null;
        connection =BaseDao.getConnection();
        int billCount=-1;
        if (connection!=null){
            try {
                connection.setAutoCommit(false);
                //判断订单是否为空
                billCount = billDao.getBillCountByProviderId(connection, pId);
                if (billCount==0){
                    providerDao.deleteProvider(connection,pId);
                }
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
                billCount=-1;
                try {
                    connection.rollback();
                } catch (SQLException E) {
                    E.printStackTrace();
                }
            }finally {
                BaseDao.closeResource(connection,null,null);
            }
        }
        return billCount;
    }
    /**
     * 修改供应商信息
     * **/
    public int updateProvider(Provider provider) {
        Connection connection=null;
        int flag=0;
        connection = BaseDao.getConnection();
        if (connection!=null){
            flag= providerDao.updateProvider(connection, provider);
        }
        BaseDao.closeResource(connection,null,null);
        return flag;
    }

    @Test
    public  void test(){
        ProviderService providerService=new ProviderServiceImpl();
        List<Provider> providerList = providerService.getProviderList(null, "兴化佳美调味品厂");
        System.out.println(providerList);
    }
}
